//
//  TextInputView.m
//  奇阅
//
//  Created by 奇阅科技 on 14-11-11.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "TextInputView.h"
#define KEYBOARD_H 216
#define INPUTVIEW_H 146
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define RGB_COLOR(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
@interface TextInputView()
{
    UIView *inputView;
    UITextView *textView;
    UIView *myBgViewAlpha;
}
@end
@implementation TextInputView
-(void)dealloc{
    [self NotificaionCenter:NO];
    self.stringBlock = nil;
}
//键盘通知注册（yes）和移除（no）
-(void)NotificaionCenter:(BOOL)type
{
    if (type)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    else
    {
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification
                                                     object:nil];
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification  object:nil];
    }
}
-(instancetype)initWithTitle:(NSString *)title textString:(UpdateTextString)textString{
    self = [super init];
    if (self) {
        [self initSubViewsWithTitle:title];
        self.stringBlock = textString;
    }
    return self;
}
-(void)initSubViewsWithTitle:(NSString *)title{
    self.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);
    myBgViewAlpha = [[UIView alloc]initWithFrame:self.bounds];
    myBgViewAlpha.backgroundColor = [UIColor blackColor];
    myBgViewAlpha.alpha = 0.8;
    [self addSubview:myBgViewAlpha];
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myButton.frame = self.bounds;
    [myButton addTarget:self action:@selector(hidenkeyboard) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:myButton];
    
    inputView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, INPUTVIEW_H)];
    inputView.backgroundColor = RGB_COLOR(235, 235, 240);
    [self addSubview:inputView];
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    closeButton.frame = CGRectMake(0, 0, 64, 64);
    [closeButton setImage:[UIImage imageNamed:@"guanbibutton1"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(hidenInputView) forControlEvents:UIControlEventTouchUpInside];
    [inputView addSubview:closeButton];
    
    
    UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    updateButton.frame = CGRectMake(SCREEN_WIDTH-64, 0, 64, 64);
    [updateButton setImage:[UIImage imageNamed:@"quedingbutton1"] forState:UIControlStateNormal];
    [updateButton addTarget:self action:@selector(updateTextString) forControlEvents:UIControlEventTouchUpInside];
    [inputView addSubview:updateButton];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((SCREEN_WIDTH/2)-100, 20, 200, 20)];
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:16];
    label.textColor = RGB_COLOR(85, 85, 85);
    [inputView addSubview:label];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 52, SCREEN_WIDTH-30, 80)];
    imageView.image = [UIImage imageNamed:@"commentInputLine"];
    [inputView addSubview:imageView];
    
    textView = [[UITextView alloc]initWithFrame:CGRectMake(17, 54, SCREEN_WIDTH-34, 76)];
    textView.font = [UIFont systemFontOfSize:14];
    textView.backgroundColor = [UIColor whiteColor];
    [inputView addSubview:textView];
    _commentTextView = textView;
}
#pragma mark - 设置输入框的背景颜色
-(void)setInputViewBackgroundColor:(UIColor *)backgroundColor{
    inputView.backgroundColor = backgroundColor;
}
#pragma mark - 显示输入框
-(void)showInputView{
    if (!self) {
        [self initSubViewsWithTitle:@"写评论"];
    }
    [self NotificaionCenter:YES];
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    myBgViewAlpha.alpha = 0;
    inputView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, INPUTVIEW_H);
    [UIView animateWithDuration:0.3 animations:^{
        myBgViewAlpha.alpha = 0.8;
        inputView.frame = CGRectMake(0, SCREEN_HEIGHT-INPUTVIEW_H, SCREEN_WIDTH, INPUTVIEW_H);
        [textView becomeFirstResponder];
    }];
}
#pragma mark - 移除输入框
- (void)hidenInputView{
    [self NotificaionCenter:NO];
    [textView resignFirstResponder];
    [UIView animateWithDuration:0.35 animations:^{
        inputView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, INPUTVIEW_H);
        myBgViewAlpha.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark 移除键盘
- (void)hidenkeyboard{
    if ([textView isFirstResponder]) {
        [textView resignFirstResponder];
    } else {
        [self hidenInputView];
    }
    
}
#pragma mark - 点击确定按钮返回输入内容
-(void)updateTextString{
    if (textView.text.length>1) {
        if (self.stringBlock) {
            self.stringBlock(textView.text);
            textView.text = @"";
        }
    }
    [self hidenInputView];
}

#pragma mark - 键盘高度代理
//键盘的显示隐藏
-(void)keyboardWillShow:(NSNotification *)note
{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        inputView.frame = CGRectMake(0, SCREEN_HEIGHT-keyboardBounds.size.height-INPUTVIEW_H, SCREEN_WIDTH, INPUTVIEW_H);
    }];
}
-(void) keyboardWillHide:(NSNotification *)note
{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        inputView.frame = CGRectMake(0, SCREEN_HEIGHT-INPUTVIEW_H, SCREEN_WIDTH, INPUTVIEW_H);
    }];
}

@end
